<?php

class Adept_Component_SelectItemsArray extends ArrayObject
{
    
    /**
     * @param Adept_Component_Composite $parent
     */
    public function __construct($parent) 
    {
        parent::__construct($this->fetch($parent));
    }	

    /**
     * @param Adept_Component_Composite $parent
     */    
    protected function fetch($parent)
    {
        $result = array();
        
        foreach ($parent->getChildren() as $child) {
            if ($child instanceof Adept_Component_SelectItem) {
                $result[] = new Adept_Model_SelectItem($child->getValue(), $child->getTitle(), $child->isDisabled());
            } elseif ($child instanceof Adept_Component_SelectItems) {
                $values = $child->getValues();
                foreach ($values as $key => $value) {
                    if ($value instanceof Adept_Model_SelectItem) {
                        $result[] = $value;
                    } elseif (is_array($value)) {
                        $result[] = new Adept_Model_SelectItem(
                            isset($value['value']) ? $value['value'] : '',
                            isset($value['title']) ? $value['title'] : null,
                            isset($value['description']) ? $value['description'] : null,
                            isset($value['disabled']) ? $value['disabled'] : false
                        );
                    } elseif (is_object($value)) {
                        
                        $valueOfItem = Adept_Binding_Util::getPropertyValue($value, $child->getValueProperty());
                        $titleOfItem = Adept_Binding_Util::getPropertyValue($value, $child->getTitleProperty());
                        
                        $result[] = new Adept_Model_SelectItem($valueOfItem, $titleOfItem);
                        
                    } else {
                        $result[] = new Adept_Model_SelectItem($key, $value);
                    }
                }
            }
        }
        return $result;
    }
    
}

